home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac-Source 1994 July
/
Mac-Source_July_1994.iso
/
Apple Script
/
OSAX
/
Four Password OSAXen
/
PassWord3 Folder
/
PassWord3.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-05-14
|
5KB
|
180 lines
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Password3.c
// Written by: Donald Olson
// May 1993
// Copyright ® 1993 Apple Computer Inc.
// All rights reserved.
//
// This is a simple Scripting Addition that asks the user to type in a
// password. This is different from PassWord.c in that the users
// password is stored in the refCon field so that it only needs to be
// entered once. In addition, this version has an optional parameter
// that clears the stored password. This was written for a talk at the
// 1993 WWDC given by Donald Olson and Donn Denman.
//
// Syntax: User Password3 [Clear]
// Return: Encrypted form of user password or cancel.
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <string.h>
#include <Dialogs.h>
#include <AppleEvents.h>
#include <Memory.h>
// Our headers
void FrameDefault( DialogPtr theDialog, long theItem);
pascal Boolean MyDlgFilter( DialogPtr theDlg,
EventRecord *theEventRec, short *itemHit);
void EncryptString(StringPtr strPtr);
// Our constants
#define DLOGID 128
#define MYClass 'OLIE'
#define MyID 'psw3'
#define OurEnum 'cler'
pascal OSErr main(AppleEvent *theEvent,
AppleEvent *theReply,
long theRefCon) {
DialogPtr theDialog = nil;
OSErr theErr = noErr;
GrafPtr savedPort;
short itemHit;
Str32 theString;
StringPtr theStrPtr = (StringPtr)&theString;
StringHandle theStrHdl = (StringHandle)&theStrPtr;
EventHandlerProcPtr handler;
long handlerRefcon, actualSize;
Handle ourGlobalHdl = nil;
DescType typeCode;
long theEnum = 0;
// Get the direct object, if any.
theErr = AEGetParamPtr(theEvent, keyDirectObject,
typeEnumerated, &typeCode,
(Ptr)&theEnum, sizeof(theEnum), &actualSize);
if(OurEnum == theEnum) {
if(theRefCon == 0) // We've got no stored password.
return noErr;
theErr = AEGetEventHandler(MYClass, MyID,
&handler, &handlerRefcon, true);
DisposeHandle((Handle)handlerRefcon); // Get rid of our handle
theErr = AEInstallEventHandler(MYClass, MyID,
handler, 0, true);
}
if(theRefCon != 0) // We've got a stored password.
goto ADDPARAM;
GetPort(&savedPort);
theErr = AEInteractWithUser(kAEDefaultTimeout, nil, nil);
if(theErr != noErr) return theErr;
theDialog = GetNewDialog(DLOGID, nil, (WindowPtr)-1);
if(theDialog == nil) return resNotFound;
SetPort(theDialog);
FrameDefault(theDialog, ok);
strcpy((char*)theString, (char*) "\0"); // Empty string
SetWRefCon(theDialog, (long)theStrHdl);
do {
ModalDialog((ModalFilterProcPtr)MyDlgFilter, &itemHit);
} while (itemHit != cancel && itemHit != ok);
if(itemHit == cancel) {
DisposDialog(theDialog);
return userCanceledErr;
}
DisposDialog(theDialog);
SetPort(savedPort);
EncryptString(theStrPtr);
theErr = AEGetEventHandler(MYClass, MyID,
&handler, &handlerRefcon, true);
ourGlobalHdl = NewHandleSysClear(sizeof(theString));
if(ourGlobalHdl == nil) return MemError();
HLock((Handle)theStrHdl);
//theErr = HandAndHand((Handle)theStrHdl, ourGlobalHdl);
BlockMove((Ptr)*theStrHdl, (Ptr)*ourGlobalHdl, sizeof(theString));
HUnlock((Handle)theStrHdl);
if(theErr != noErr)
return theErr;
theErr = AEInstallEventHandler( MYClass, MyID,
handler, (long)ourGlobalHdl, true);
theRefCon = (long)ourGlobalHdl; // For our local use.
ADDPARAM:;
HLock((Handle)theRefCon);
theErr = AEPutParamPtr( theReply, keyDirectObject, typeChar,
(Ptr)*(Handle)theRefCon, strlen((char*)*(Handle)theRefCon));
HUnlock((Handle)theRefCon);
return theErr;
}
void EncryptString(StringPtr strPtr) {
short theSize, x;
theSize = strlen((char*)(strPtr));
for( x = 0; x <= (theSize -1); x++) {
strPtr[x] = (short)strPtr[x] + (short)"\¡";
}
}
pascal Boolean MyDlgFilter(DialogPtr theDlg,
EventRecord *theEventRec,
short *itemHit)
{
StringHandle theStrHdl;
short theSize;
if(theEventRec->what != keyDown) // Just looking for keystrokes
return false;
switch((theEventRec->message) & charCodeMask)
{
case 0x0d: // Return
*itemHit = ok;
return true;
case 0x03: // Enter
*itemHit = ok;
return true;
case 0x08: // Backspace
return false;
default:
theStrHdl = (StringHandle)GetWRefCon(theDlg);
theSize = strlen((char*)(*theStrHdl));
(*theStrHdl)[theSize] = (char)(theEventRec->message);
(*theStrHdl)[theSize + 1] = (char)NULL;
theEventRec->message = '•';
return false;
}
}
void FrameDefault( DialogPtr theDialog, long theItem) {
short DType;
Handle DItem;
Rect DRect;
GetDItem(theDialog, theItem, &DType, &DItem, &DRect);
PenSize(3, 3);
InsetRect(&DRect, -4, -4);
FrameRoundRect(&DRect, 16, 16);
PenSize(1, 1);
}